package cn.com.ddapp.ddm.ui.mine;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import org.json.JSONObject;

import java.net.URLDecoder;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.mine.UserData;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.widget.CircleImageView;
import jp.line.android.sdk.model.User;
import okhttp3.Call;

public class MyMoneyActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.headimg)
    CircleImageView headimg;
    @BindView(R.id.pay)
    LinearLayout pay;
    @BindView(R.id.moneyTv)
    TextView money;
//    @BindView(R.id.withdraw_deposit)
//    LinearLayout withdrawDeposit;G980910030010072

    @BindView(R.id.detail)
    LinearLayout detail;
    String headImg, wallet;
    int MONEY = 301;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.empty)
    EmptyLayout empty;

    String userId;
    Bitmap mineBitmap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        wallet = intent.getStringExtra("wallet");
        money.setText(NetUtils.setDecimal(Double.parseDouble(wallet))+"");
        headImg = intent.getStringExtra("sheadImg");
        Log.e("111mymoney",headImg);
        mineBitmap = intent.getParcelableExtra("bitmap");
        /*Glide.with(this).load(OKUtilAPI.imgBaseUrl + headImg)
                .placeholder(R.mipmap.src)
                .into(headimg);*/
        if (mineBitmap == null){
            if (!TextUtils.isEmpty(headImg)) {
                if(headImg.contains("http")){
                    Glide.with(this).load(headImg).into(headimg);
                }else {
                    Glide.with(this).load(OKUtilAPI.imgBaseUrl + headImg).into(headimg);
                }
            }
        }
        else {
            headimg.setImageBitmap(mineBitmap);
        }
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        userId = SPUtils.getStringData(this, SPUtils.USERID);
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    UserData userData = gson.fromJson(response, UserData.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                        // mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                        BaseApplication.getIntstance().showToast(userData.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else {
                        if (userData.getUser() != null) {
                            double wallet = userData.getUser().getWallet();
                            money.setText(NetUtils.setDecimal(wallet) + "");
                            Intent intent = new Intent();
                            intent.putExtra("money",wallet+"");
                            setResult(8,intent);
                        }
                    }
                }
            };
            //请求会员资料
            OKUtilAPI.post(OKUtilAPI.getUser, "userId=" + userId, callback);

            empty.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback3 = new OkUtilCallback(empty) {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if ("OK".equals(jsonObject.getString("result"))) {
                            if (!jsonObject.isNull("content")){
                                String content = jsonObject.getString("content");
                                webView.loadData(content, "text/html;charset=UTF-8", null);
                            }
                            else {
                                empty.setErrorType(EmptyLayout.NODATA);
                            }
                        }
                    } catch (Exception e) {
                        empty.setErrorType(EmptyLayout.NETWORK_ERROR);
                    }
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    empty.setErrorType(EmptyLayout.NETWORK_ERROR);
                    super.onError(call, e, id);
                }
            };
            //规则说明
            OKUtilAPI.post(OKUtilAPI.getART, "type=5", callback3);
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_my_money;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

//    @OnClick({R.id.back, R.id.pay, R.id.withdraw_deposit, R.id.detail,R.id.webView,R.id.img_error_layout})
    @OnClick({R.id.back, R.id.pay,  R.id.detail,R.id.webView,R.id.img_error_layout})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.webView:
                initData();
                break;
            case R.id.pay:
                intent = new Intent(this, PayActivity.class);
                startActivity(intent);
                break;
//            case R.id.withdraw_deposit:
//                intent = new Intent(this, WithdrawDepositActivity.class);
//                intent.putExtra("money", money.getText().toString());
//                startActivity(intent);
//                //startActivityForResult(intent, MONEY);
//                break;
            case R.id.detail:
                intent = new Intent(this, DetailActivity.class);
                startActivity(intent);
                break;
            case R.id.img_error_layout:
                initData();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    /*@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MONEY && resultCode == 505) {
            String m = data.getStringExtra("money");
            this.money.setText(NetUtils.setDecimal(Double.parseDouble(m))+"");
            Intent intent = new Intent();
            intent.putExtra("money",m);
            setResult(8,intent);
        }
    }*/
}
